<template>
    <div>
        <el-form :inline="true" :model="form" align="left" class="demo-form-inline">
            <el-form-item label="移库单号：">
                <el-input v-model="form.RelocationCode" placeholder="移库单号"></el-input>
            </el-form-item>
            <el-form-item label="移库类型">
                <el-select v-model="form.RelocationType" placeholder="移库类型">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="移库上架" value="移库上架"></el-option>
                    <el-option label="仓库移库" value="仓库移库"></el-option>
                    <el-option label="移库报损" value="移库报损"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="移库状态">
                <el-select v-model="form.State" placeholder="移库状态">
                    <el-option label="全部" :value="0"></el-option>
                    <el-option label="待审核" :value="1"></el-option>
                    <el-option label="审核成功" :value="2"></el-option>
                    <el-option label="审核失败" :value="3"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="GetRelocationList">查询</el-button>
                <el-button icon="el-icon-refresh-left" @click="result">重置</el-button>
                <el-button type="primary" icon="el-icon-refresh-left" @click="add">新增</el-button>
                <el-button type="danger" icon="el-icon-refresh-left" @click="DelAll">批量删除</el-button>
                <el-button @click="exportListToExcel(Relocation)">导出</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="Relocation" border style="width: 200%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" fixed>
            </el-table-column>
            <el-table-column prop="relocationCode" label="移库单号" width="150" fixed>
            </el-table-column>
            <el-table-column prop="productName" label="产品名称" fixed>
            </el-table-column>
            <el-table-column prop="productCode" label="产品编号" width="150" fixed>
            </el-table-column>
            <el-table-column prop="batch" label="批次">
            </el-table-column>
            <el-table-column prop="unitName" label="单位">
            </el-table-column>
            <el-table-column prop="number" label="数量">
            </el-table-column>
            <el-table-column prop="oldWarehouseName" label="原仓库" width="150">
            </el-table-column>
            <el-table-column prop="oldLocationName" label="原库位" width="150">
            </el-table-column>
            <el-table-column prop="newWarehouseName" label="新仓库" width="150">
            </el-table-column>
            <el-table-column prop="newLocationName" label="新库位" width="150">
            </el-table-column>
            <el-table-column prop="relocationType" label="移库类型" width="150">
            </el-table-column>
            <el-table-column prop="state" label="状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.state == 1">待审核</span>
                    <span v-if="scope.row.state == 2">审核成功</span>
                    <span v-if="scope.row.state == 3">审核失败</span>
                </template>
            </el-table-column>
            <el-table-column prop="reviewer" label="审核人">
            </el-table-column>
            <el-table-column prop="audittime" label="审核时间" width="210">
                <template slot-scope="scope">
                    <span>{{ scope.row.audittime.substring(0, 20).replace('T', ' ') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="view(scope.row.id)">查看</el-button>
                    <el-button type="text" size="small" @click="Edit(scope.row.relocationCode)">修改</el-button>
                    <el-button type="text" size="small" v-if="scope.row.state == 1 || scope.row.state == 3"
                        @click="del(scope.row.relocationCode)">删除</el-button>
                    <el-button type="text" v-if="scope.row.state == 1" size="small"
                        @click="audit(scope.row.id)">审核</el-button>
                    <el-button type="text" v-if="scope.row.state == 3" size="small"
                        @click="audit(scope.row.id)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>


        <!-- 弹框 -->
        <el-dialog title="移库单详情" :visible.sync="dialogFormVisible">
            <!-- 用户类型 -->
            <el-descriptions v-model="RelocationV">
                <el-descriptions-item label="移库编号">{{
                    RelocationV.relocationCode
                }}</el-descriptions-item>
                <el-descriptions-item label="移库类型">
                    {{ RelocationV.relocationType }}
                </el-descriptions-item>
                <el-descriptions-item label="单据状态">
                    <span v-if="RelocationV.state == 1">待审核</span>
                    <span v-if="RelocationV.state == 2">审核成功</span>
                    <span v-if="RelocationV.state == 3">审核失败</span>
                </el-descriptions-item>
                <el-descriptions-item label="制单人">{{
                    RelocationV.creator
                }}</el-descriptions-item>
                <el-descriptions-item label="制单日期">{{
                    RelocationV.creatorTime
                }}</el-descriptions-item>
                <el-descriptions-item label="备注">{{
                    RelocationV.remarks
                }}</el-descriptions-item>
                <el-descriptions-item label="审核原因">{{
                    RelocationV.opinion
                }}</el-descriptions-item>
            </el-descriptions>
            <el-table border style="width: 100%" :data="RelocationList">
                <el-table-column prop="productCode" label="产品编码"></el-table-column>
                <el-table-column prop="productName" label="产品名称">
                </el-table-column>
                <el-table-column prop="unitName" label="单位"> </el-table-column>
                <el-table-column prop="number" label="数量"> </el-table-column>
                <el-table-column prop="batch" label="批次"> </el-table-column>
                <el-table-column prop="oldLocationName" label="当前库位"> </el-table-column>
                <el-table-column prop="newLocationName" label="移入库位"> </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogFormVisible = false">关闭</el-button>
            </div>
        </el-dialog>

        <!-- 弹框 -->
        <el-dialog title="审核" :visible.sync="dialogFormVisible1">
            <!-- 用户类型 -->
            <el-descriptions v-model="RelocationV">
                <el-descriptions-item label="移库编号">{{
                    RelocationV.relocationCode
                }}</el-descriptions-item>
                <el-descriptions-item label="移库类型">
                    {{ RelocationV.relocationType }}
                </el-descriptions-item>
                <el-descriptions-item label="单据状态">
                    <span v-if="RelocationV.state == 1">待审核</span>
                    <span v-if="RelocationV.state == 2">审核成功</span>
                    <span v-if="RelocationV.state == 3">审核失败</span>
                </el-descriptions-item>
                <el-descriptions-item label="制单人">{{
                    RelocationV.creator
                }}</el-descriptions-item>
                <el-descriptions-item label="制单日期">{{
                    RelocationV.creatorTime
                }}</el-descriptions-item>
                <el-descriptions-item label="备注">
                    {{ RelocationV.remarks }}
                </el-descriptions-item>
                <el-descriptions-item label="审核原因">
                    <el-input type="textarea" v-model="RelocationV.opinion" maxlength="300"></el-input>
                </el-descriptions-item>
            </el-descriptions>
            <el-table border style="width: 100%" :data="RelocationList">
                <el-table-column prop="productCode" label="产品编码"></el-table-column>
                <el-table-column prop="productName" label="产品名称">
                </el-table-column>
                <el-table-column prop="unitName" label="单位"> </el-table-column>
                <el-table-column prop="number" label="数量"> </el-table-column>
                <el-table-column prop="batch" label="批次"> </el-table-column>
                <el-table-column prop="oldLocationName" label="当前库位"> </el-table-column>
                <el-table-column prop="newLocationName" label="移入库位"> </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="Ateaudit">审核通过</el-button>
                <el-button type="danger" @click="Ateaudit1">审核不通过</el-button>
                <el-button type="primary" @click="dialogFormVisible1 = false">关闭</el-button>
            </div>
        </el-dialog>

        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="form.PageIndex"
            :page-sizes="[2, 5, 10, 20]" :page-size="form.PageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="form.totalCount" prev-text="上一页" next-text="下一页">
        </el-pagination>
    </div>
</template>
<script>
import { GetRelocationList, GetRelocationview, GetRelocation, DeleteRelocationList, DeleteRelocation, UpdAteaudit, UpdAteaudit1 } from "@/api/WarehouseServer/Relocation";
export default {
    data() {
        return {
            CheckList: [],
            Relocation: [],
            RelocationV: {},
            RelocationList: [],
            dialogFormVisible: false,
            dialogFormVisible1: false,
            form: {
                RelocationCode: "",
                RelocationType: "",
                State: 0,
                PageIndex: 1,
                PageSize: 5,
                pageCount: 0,
                totalCount: 0,
            },
        }
    },
    methods: {
        //修改
        Edit(relocationCode) {
            this.$router.push("RelocationEdit?relocationCode=" + relocationCode);
        },
        //审核
        audit(Id) {
            this.dialogFormVisible1 = true;
            this.Id = Id;
            this.Getrelocationview();
            this.GetRelocation();
        },
        //审核通过
        Ateaudit() {
            var data = {
                id: this.Id,
                relocationCode: this.RelocationV.relocationCode,
                relocationType: this.RelocationV.relocationType,
                creator: this.RelocationV.creator,
                createTime: this.RelocationV.createTime,
                remarks: this.RelocationV.remarks,
                opinion: this.RelocationV.opinion,
                state: 2,
                reviewer: localStorage.getItem("encode")
            };
            UpdAteaudit(data).then(res => {
                if (res.data.totalCount > 0) {
                    this.$message({
                        message: '审核成功',
                        position: "bottom-right",
                        duration: 900,
                        type: 'success'
                    });
                    this.form.PageIndex = 1;
                    this.GetRelocationList();
                    this.dialogFormVisible1 = false;
                } else {
                    this.$message({
                        message: '审核失败',
                        position: "bottom-right",
                        duration: 900,
                        type: 'error'
                    });
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    position: "bottom-right",
                    duration: 900,
                    message: '已取消审核'
                });
            });
        },
        //审核不通过
        Ateaudit1() {
            var data = {
                id: this.Id,
                relocationCode: this.RelocationV.relocationCode,
                relocationType: this.RelocationV.relocationType,
                creator: this.RelocationV.creator,
                createTime: this.RelocationV.createTime,
                remarks: this.RelocationV.remarks,
                opinion: this.RelocationV.opinion,
                state: 3,
                reviewer: localStorage.getItem("encode")
            };
            UpdAteaudit1(data).then(res => {
                if (res.data.totalCount > 0) {
                    this.$message({
                        message: '审核成功',
                        position: "bottom-right",
                        duration: 900,
                        type: 'success'
                    });
                    this.form.PageIndex = 1;
                    this.GetRelocationList();
                    this.dialogFormVisible1 = false;
                } else {
                    this.$message({
                        message: '审核失败',
                        position: "bottom-right",
                        duration: 900,
                        type: 'error'
                    });
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    position: "bottom-right",
                    duration: 900,
                    message: '已取消审核'
                });
            });
        },
        //批量删除
        DelAll() {
            if (this.CheckList.length == 0) {
                this.$notify({
                    title: "警告",
                    message: "请至少选择一条数据",
                    position: "bottom-right",
                    type: "warning",
                    duration: 900,
                });
                return;
            }
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    DeleteRelocationList(this.CheckList).then((res) => {
                        if (res.data.totalCount > 0) {
                            this.$notify({
                                title: "成功提示",
                                message: "批量删除成功",
                                position: "bottom-right",
                                type: "success",
                            });
                            this.form.PageIndex = 1;
                            this.GetRelocationList();
                        } else {
                            this.$notify({
                                title: "错误提示",
                                message: "批量删除失败",
                                position: "bottom-right",
                                type: "error",
                            });
                            return;
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        //删除
        del(relocationCode) {
            this.$confirm('是否删除该采购单?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var data = {
                    relocationCode: relocationCode
                };
                DeleteRelocation(data).then(res => {
                    if (res.data.totalCount > 0) {
                        this.$message({
                            message: '删除成功',
                            position: "bottom-right",
                            duration: 900,
                            type: 'success'
                        });
                        this.form.pageIndex = 1;
                        this.GetRelocationList();
                    } else {
                        this.$message({
                            message: '删除失败',
                            position: "bottom-right",
                            duration: 900,
                            type: 'error'
                        });
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        position: "bottom-right",
                        duration: 900,
                        message: '已取消删除'
                    });
                });
            })
        },
        //查看
        view(Id) {
            this.dialogFormVisible = true;
            this.Id = Id;
            this.Getrelocationview();
            this.GetRelocation();
        },
        Getrelocationview() {
            var data = {
                id: this.Id,
            };
            GetRelocationview(data).then(res => {
                this.RelocationV = res.data;
            });
        },
        GetRelocation() {
            var data = {
                id: this.Id,
            };
            GetRelocation(data).then(res => {
                this.RelocationList = res.data.data;
            })
        },
        //导出
        exportListToExcel(dataList) {
            // 过滤需要导出的字段
            const filteredData = dataList.map(item => {
                return {
                    编号: item.id,
                    移库单号: item.relocationCode,
                    移库单类型: item.relocationType,
                    审核状态: item.state,
                    审核人: item.reviewer,
                    审核意见: item.opinion,
                    审核时间: item.audittime,
                    产品编号: item.productCode,
                    产品名称: item.productName,
                    单位名称: item.unitName,
                    批次: item.batch,
                    移库数量: item.number,
                    原仓库: item.oldWarehouseName,
                    原库位: item.oldLocationName,
                    移入仓库: item.newWarehouseName,
                    移入库位: item.newLocationName,
                };
            });
            const worksheet = XLSX.utils.json_to_sheet(filteredData);
            const workbook = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
            // 导出Excel文件   
            XLSX.writeFile(workbook, '移库管理.xlsx');
        },
        //跳转到新增页面
        add() {
            this.$router.push({ path: "RelocationAdd" });
        },
        // 多选
        handleSelectionChange(val) {
            this.CheckList = val;
        },
        //重置
        result() {
            this.form.RelocationCode = "";
            this.form.RelocationType = "",
                this.form.State = 0;
        },
        //显示
        GetRelocationList() {
            var data = {
                RelocationCode: this.form.RelocationCode,
                RelocationType: this.form.RelocationType,
                State: this.form.State,
                PageIndex: this.form.PageIndex,
                PageSize: this.form.PageSize,
            };
            GetRelocationList(data).then(res => {
                this.Relocation = res.data.data;
                this.form.pageCount = res.data.pageCount;
                this.form.totalCount = res.data.totalCount;
            });
        },
        //分页
        handleSizeChange(val) {
            this.form.PageSize = val;
            this.GetRelocationList();
        },
        handleCurrentChange(val) {
            this.form.PageIndex = val;
            this.GetRelocationList();
        },
    },
    mounted() {
        this.GetRelocationList();
    },
}
</script>
<style></style>